package course.d1229;
/*

 */
public class p2 {
    public static void main(String[] args) {
        BaoZi baoZi = new BaoZi();
        //创建顾客线程
        new Customer(baoZi).start();
        new BaoZiStore(baoZi).start();
    }
}
class BaoZi {
    private String pi;
    private String xian;
    //包子的状态 true：存在包子  false：不存在包子
    private boolean isExist;

    public String getPi() {
        return pi;
    }

    public void setPi(String pi) {
        this.pi = pi;
    }

    public String getXian() {
        return xian;
    }

    public void setXian(String xian) {
        this.xian = xian;
    }

    public boolean isExist() {
        return isExist;
    }

    public void setExist(boolean exist) {
        isExist = exist;
    }
}

class Customer extends Thread{

    private BaoZi baoZi;

    public Customer(BaoZi baoZi) {
        this.baoZi = baoZi;
    }

    @Override
    public void run() {
        while (true){

            synchronized (baoZi){
                //判断包子存不存在
                if (!(baoZi.isExist())){
                    //顾客进入等待
                    try {
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //包子存在
                // 吃包子
                System.out.println("顾客开始吃"+baoZi.getPi()+baoZi.getXian()+"包子，吃了三秒钟");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //把包子的状态设置为false
                baoZi.setExist(false);
                //包子吃完了，通知老板做包子
                System.out.println("包子吃完了");
                System.out.println("通知老板做包子");
                baoZi.notify();
            }
        }
    }
}
class BaoZiStore extends Thread {
    private BaoZi baoZi;

    public BaoZiStore(BaoZi baoZi) {
        this.baoZi = baoZi;
    }

    @Override
    public void run() {
        //记录包子铺做了多少个包子
        int count = 0;
        while (true){
        synchronized (baoZi){
                //判断包子的状态，有包子就等待
                if (baoZi.isExist()){
                    try {
                        //老板等待
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //包子不存在，开始做包子
                System.out.println("老板开始做包子");
                if (count%3==1){
                    baoZi.setPi("皮薄");
                    baoZi.setXian("麻辣豆腐");
                }else if (count%3==2){
                    baoZi.setPi("皮薄");
                    baoZi.setXian("鲜肉");
                }
                else {
                    baoZi.setPi("皮薄");
                    baoZi.setXian("龙虾");
                }
                count++;

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

                //把包子的状态设置为有包子
                baoZi.setExist(true);
                //通知顾客吃包子
                baoZi.notify();
            }
        }
    }
}
